package com.example.android.ouactivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.ouactivity.model.Content;
import com.example.android.ouactivity.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * create by chqing
 * 文章内容页面
 */
public class ContentActivity extends AppCompatActivity {
    private WebView contentView;
    private TextView title,author,date;
    private Content content;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String result = msg.obj.toString();
            Gson gson = new Gson();
            content = gson.fromJson(result,Content.class);

            //WebView加载html数据
            contentView.getSettings().setDefaultTextEncodingName("utf-8");
            contentView.loadData(content.getContent(),"text/html;charset=UTF-8",null);
            title.setText(content.getTitle());
            author.setText(content.getAuthor());
            date.setText(content.getDate());
//            new Handler().postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    getWindow().invalidatePanelMenu(Window.FEATURE_OPTIONS_PANEL);
//                }
//            },500);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        //设置返回，及页面标题
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        if (actionBar != null) {
            actionBar.setTitle("详情");
        }
        //实例化
        title = (TextView)findViewById(R.id.con_title);
        author = (TextView)findViewById(R.id.con_author);
        date = (TextView)findViewById(R.id.con_date);
        contentView = (WebView)findViewById(R.id.con_content);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String id = bundle.getString("id");
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.getNewsJSON("http://www.chqing.cn/content?act=cont&id="+id,handler);

    }
    //上边工具栏的各种监听
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();//就这样就可以了
                break;
            case R.id.action_comm:
                Intent commList = new Intent(ContentActivity.this,CommentListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("news",String.valueOf(content.getId()));
                commList.putExtras(bundle);
                startActivity(commList);
                break;
            case R.id.action_supp:
                Toast.makeText(ContentActivity.this,"你赞了这篇文章",Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_con, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
//        MenuItem sup = menu.findItem(R.id.action_suppMum);
//        MenuItem comm = menu.findItem(R.id.action_commMum);
//        if(content!=null){
//            sup.setTitle("("+content.getSupport()+")");
//            comm.setTitle("("+content.getComment()+")");
//        }else{
//            sup.setTitle("(0)");
//            comm.setTitle("(0)");
//        }
        return super.onPrepareOptionsMenu(menu);
    }
}
